package cn.wit.summit.chwan.widgets.photoviewer;

import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.image.ImageInfo;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.utils.MyImageLoader;
import me.relex.photodraweeview.PhotoDraweeView;

public class ImageDetailFragment extends Fragment {
    private String mImageUrl;
    private PhotoDraweeView mImageView;
    private ProgressBar progressBar;
//    private PhotoViewAttacher mAttacher;

    public static ImageDetailFragment newInstance(String imageUrl) {
        final ImageDetailFragment f = new ImageDetailFragment();

        final Bundle args = new Bundle();
        args.putString("url", imageUrl);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageUrl = getArguments() != null ? getArguments().getString("url") : null;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);

        progressBar = (ProgressBar) v.findViewById(R.id.loading);
        mImageView = (PhotoDraweeView) v.findViewById(R.id.image);

//        mAttacher = new PhotoViewAttacher(mImageView);
//        mAttacher.setOnPhotoTapListener(new OnPhotoTapListener() {
//
//            @Override
//            public void onPhotoTap(View arg0, float arg1, float arg2) {
//                getActivity().finish();
//            }
//        });
//        mAttacher.setOnLongClickListener(this.mOnLongClickListener);

        setImageViewListener();

        return v;
    }

    private void setImageViewListener() {
//        mImageView.setOnPhotoTapListener(new me.relex.photodraweeview.OnPhotoTapListener() {
//            @Override
//            public void onPhotoTap(View view, float x, float y) {
//                try {
//                    getActivity().finish();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        mImageView.setOnViewTapListener(new me.relex.photodraweeview.OnViewTapListener() {
            @Override
            public void onViewTap(View view, float v, float v1) {
                try {
                    getActivity().finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mImageView.setOnLongClickListener(this.mOnLongClickListener);
    }

    private void loadFresco() {
        try {
            progressBar.setVisibility(View.VISIBLE);

            ControllerListener controllerListener = new ControllerListener<ImageInfo>() {
                @Override
                public void onSubmit(String id, Object callerContext) {

                }

                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    progressBar.setVisibility(View.GONE);
//                    mAttacher.update();

                    mImageView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }

                @Override
                public void onIntermediateImageSet(String id, ImageInfo imageInfo) {

                }

                @Override
                public void onIntermediateImageFailed(String id, Throwable throwable) {

                }

                @Override
                public void onFailure(String id, Throwable throwable) {
                    String message = null;
                    try {
                        message = "加载失败";
                        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                    } catch (Exception e1) {
                    }
                    mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    mImageView.setImageResource(R.drawable.main_normal_icon);
                    progressBar.setVisibility(View.GONE);
//                    mAttacher.update();
                }

                @Override
                public void onRelease(String id) {

                }
            };

            DraweeController draweeController = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener)
                    .setUri(MyImageLoader.makeUri(mImageUrl))
                    .build();
            mImageView.setController(draweeController);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadGlide() {
//
//            DrawableTypeRequest<String> drawableTypeRequest = Glide.with(this).load(mImageUrl);
//            if (mImageUrl.endsWith(".gif")) {
//                drawableTypeRequest.asGif();
//            }
//
//            ImageViewTarget<GlideDrawable> target = new ImageViewTarget<GlideDrawable>(mImageView) {
//                @Override
//                public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                    super.onLoadFailed(e, errorDrawable);
//                    String message = null;
//                    try {
//                        message = "加载失败";
//                        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
//                    } catch (Exception e1) {
//                    }
//                    mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                    mImageView.setImageResource(R.drawable.main_normal_icon);
//                    progressBar.setVisibility(View.GONE);
//                    mAttacher.update();
//                }
//
//                @Override
//                protected void setResource(GlideDrawable glideDrawable) {
//                    Logger.log("ImageViewTarget", "call method setResource");
//                    view.setImageDrawable(glideDrawable);
//                    progressBar.setVisibility(View.GONE);
//                    mAttacher.update();
//                }
//            };
//            Request request = new Request() {
//                @Override
//                public void begin() {
//                    Logger.log("Request", "call method begin");
//                }
//
//                @Override
//                public void pause() {
//                    Logger.log("Request", "call method pause");
//                }
//
//                @Override
//                public void clear() {
//                    Logger.log("Request", "call method clear");
//                }
//
//                @Override
//                public boolean isPaused() {
//                    Logger.log("Request", "call method isPaused");
//                    return false;
//                }
//
//                @Override
//                public boolean isRunning() {
//                    Logger.log("Request", "call method isRunning");
//                    return false;
//                }
//
//                @Override
//                public boolean isComplete() {
//                    Logger.log("Request", "call method isComplete");
//                    progressBar.setVisibility(View.GONE);
//                    mAttacher.update();
//                    return false;
//                }
//
//                @Override
//                public boolean isResourceSet() {
//                    Logger.log("Request", "call method isResourceSet");
//                    return false;
//                }
//
//                @Override
//                public boolean isCancelled() {
//                    Logger.log("Request", "call method isCancelled");
//                    return false;
//                }
//
//                @Override
//                public boolean isFailed() {
//                    Logger.log("Request", "call method isFailed");
//                    return false;
//                }
//
//                @Override
//                public void recycle() {
//                    Logger.log("Request", "call method recycle");
//
//                }
//            };
//            target.setRequest(request);
//            drawableTypeRequest.into(target);
    }

    private void loadImageLoader() {
        /*ImageLoader.getInstance().displayImage(mImageUrl, mImageView, ImageLoderDefalutBulder.getDefaultImageLodeOption(), new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "加载失败";
                        break;
                    case DECODING_ERROR:
                        message = "图片无法显示";
                        break;
                    case NETWORK_DENIED:
                        message = "网络有问题，无法下载";
                        break;
                    case OUT_OF_MEMORY:
                        message = "图片太大无法显示";
                        break;
                    case UNKNOWN:
                        message = "未知的错误";
                        break;
                }

                try {
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                }
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                progressBar.setVisibility(View.GONE);
                mAttacher.update();
            }
        });*/
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loadFresco();
    }

    //long click
    View.OnLongClickListener mOnLongClickListener;

    void setOnLongClickListener(View.OnLongClickListener l) {
        mOnLongClickListener = l;
    }
}
